package cate.game.activity.battleroyale;

import cate.common.table.activity.battleroyale.row.BattleRoyaleGiftRow;
import cate.common.table.activity.battleroyale.row.BattleRoyaleLevelRow;
import cate.common.table.d.GDFight;
import cate.common.table.d.GDOperation;
import cate.game.activity.battleroyale.msg.BattleRoyaleChallengeResultResp;
import cate.game.activity.battleroyale.po.RoleBattleRoyale;
import cate.game.play.proce.play.PlayPO;
import cate.game.play.vo.PlayReportVO;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarmFTC;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class BattleRoyaleFTC extends AbstractFarmFTC<RoleBattleRoyale> {
    /**
     * 当前挑战关卡的模板
     */
    private final BattleRoyaleLevelRow tpl;

    public BattleRoyaleFTC(Role role, int funcId, BattleRoyaleLevelRow tpl) {
        this.funcId = funcId;
        this.tpl = tpl;
        setup(role);
    }

    @Override
    protected RoleBattleRoyale po(Role role) {
        return role.getActivity().getEnableActivitySingle(funcId);
    }

    @Override
    public void handleResult(Role role, PlayPO play) {
        super.handleResult(role, play);
        boolean win = play.winner == GDFight.Team.A;
        //是否还要继续挑战
        boolean autoEnter = false;
        final byte nextPosNum = (byte) (super.posNum + 1);
        RoleBattleRoyale battleRoyale = po(role);
        if (battleRoyale == null) {
            return;
        }
        BattleRoyaleHandler handler = role.getGame().activityGlobal.getHandler(funcId);
        if (handler == null) {
            return;
        }
        //之前的战报
        List<PlayReportVO> reports = new ArrayList<>();
        BattleRoyaleChallengeResultResp resultResp = new BattleRoyaleChallengeResultResp();
        if (win) {
            autoEnter = StringUtils.isNotBlank(tpl.getInsCode(nextPosNum));
            if (!autoEnter) {
                List<PlayPO> plays = handler.tryUpdate(role, play, tpl.chapter, tpl.level);
                battleRoyale.onPass(tpl.chapter, tpl.level);
                for (PlayPO p : plays) {
                    reports.add(new PlayReportVO(p));
                }
                battleRoyale.noticeUpdate();
                role.getHistory().action.battleRoyalePassed();
//                MixRes reward = new MixRes(tpl.rewardStr);
//                resultResp.reward = reward;
//                reward.add(role, true, GDOperation.BATTLE_ROYALE_REWARD, tpl.chapter);
            } else {
                //还没打完 先缓存一下 这一场的录像
                handler.recordPlay(role, play);
            }

        } else {
            //打输了要把之前的录像战报发过去
            List<PlayPO> plays = handler.removePlay(role);
            for (PlayPO p : plays) {
                reports.add(new PlayReportVO(p));
            }
            reports.add(new PlayReportVO(play));
            List<BattleRoyaleGiftRow> giftRows = role.getGame().table.battleRoyale.gift.giftRows(tpl.id);
            if (CollectionUtils.isNotEmpty(giftRows)) {
                giftRows.forEach(e -> role.getHistory().action.battleRoyaleFail(e.id));
            }
        }
        resultResp.funcId = funcId;
        resultResp.chapter = tpl.chapter;
        resultResp.level = tpl.level;
        resultResp.win = win;
        resultResp.over = !autoEnter;
        resultResp.reports = reports;
        role.sendNow(resultResp);

//        if(autoEnter){
//            EcResult<Void> r = battleRoyale.doChallenge(tpl.chapter, tpl.level, nextPosNum);
//            if (!r.ok()) {
//                role.getGame().farm.local.tower.removePlay(role.getUid());
//            }
//        }
    }
}
